package com.morder.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.morder.R;
import com.morder.application.BuildConfig;

public class VerifyNumberActivity extends Activity implements OnClickListener {
  private Timer mTimer;
  private TimerTask mTask;
  private int time = 1 * 1000;
  private int i = 0;
  private TextView mTimeToReget;
  private TextView mReget;
  private Button mSubmitVerifyCode;
  private EditText mVerifyCode;
  private BroadcastReceiver mReceiver;

  protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.verify_number_activity);
    mTimeToReget = (TextView) findViewById(R.id.time_to_reget);
    mTimeToReget.setVisibility(View.VISIBLE);
    
    mReget = (TextView) findViewById(R.id.reget);
    
    mSubmitVerifyCode = (Button) findViewById(R.id.submit_verify_code);
    mSubmitVerifyCode.setOnClickListener(this);
    
    mVerifyCode = (EditText) findViewById(R.id.verify_code);
    mTask = new TimerTask() {
      @Override
      public void run() {
        i++;
        Message message = new Message();
        message.what = i;
        mHandler.sendMessage(message);
      }
    };
    mTimer = new Timer(true);
    mTimer.schedule(mTask, 0, time);
    
    interfaceRefresh();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
  };

  final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case 15: {
        if (mTask != null) {
          mTask.cancel();
        }
        mTimeToReget.setVisibility(View.GONE);
        mReget.setVisibility(View.VISIBLE);
        break;
      }
      default:
        mTimeToReget.setText(15 - i + getString(R.string.sec));
        break;
      }
      super.handleMessage(msg);
    }
  };

  private void interfaceRefresh() {
    mReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("msg");
        if (msg.equals("close")) {
          finish();
        }
      }
    };
  }
  
  protected void onPause() {
    super.onPause();
    if (mTask != null) {
      mTask.cancel();
    }
  }
  
  @Override
  protected void onDestroy() {
    super.onDestroy();
    registerReceiver(mReceiver, new IntentFilter(BuildConfig.REGISTER_SUCCESS));
    unregisterReceiver(mReceiver);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.submit_verify_code:
      if (mVerifyCode.getText().length() == 0) {
        Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
      } else {
        Intent intent = new Intent(this, SetPwdActivity.class);
        startActivity(intent);
      }
      break;

    default:
      break;
    }
  };
}